import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Set;

public class zidonghuaceshi {
    public void zidonghuaceshi() throws InterruptedException, IOException {
        WebDriverManager.chromedriver().setup();
        ChromeOptions options = new ChromeOptions();

        options.addArguments("--remote-allow-origins=*");

        //打开浏览器
        WebDriver driver = new ChromeDriver(options);

        //输入百度网址
        driver.get("https://www.baidu.com");

        //设置窗口大小
        /*driver.manage().window().maximize();
        Thread.sleep(2000);

        driver.manage().window().minimize();
        Thread.sleep(2000);

        driver.manage().window().fullscreen();
        Thread.sleep(2000);

        driver.manage().window().setSize(new Dimension(1920, 1080));
        Thread.sleep(2000);*/

        //获取文本信息
        /*String text = driver.findElement(By.xpath("//*[@id=\"s-top-left\"]/a[4]")).getText();
        System.out.println(text);*/

        //获取当前页标题
        /*System.out.println(driver.getCurrentUrl());
        System.out.println(driver.getTitle());*/

        //获取当前⻚⾯句柄：
        /*String curWindow = driver.getWindowHandle();
        System.out.println("当前⻚⾯句柄："+curWindow);*/

        /*driver.findElement(By.cssSelector("#chat-textarea")).sendKeys("西南石油大学");
        driver.findElement(By.cssSelector("#chat-submit-button")).click();*/

        /*driver.findElement(By.xpath("//*[@id=\"s-top-left\"]/a[4]")).click();
        Set<String> windows = driver.getWindowHandles();
        for(String window : windows) {
            System.out.println("获取所有⻚⾯句柄："+window);
        }

        for(String window : windows) {
            if(!curWindow.equals(window)) {
                driver.switchTo().window(window);
            }
        }


        System.out.println(driver.getWindowHandle());


        Thread.sleep(3000);*/

        //清除
        /*driver.findElement(By.cssSelector("#chat-textarea")).clear();

        driver.findElement(By.cssSelector("#chat-textarea")).sendKeys("西南石油大学教务处");

        Thread.sleep(3000);




        //寻找“百度一下”搜索键进行点击
        driver.findElement(By.cssSelector("#chat-submit-button")).click();*/


        //截屏
       /* File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file,new File("screenshoot.png"));*/

        //调用截屏封装方法
        //getScreenShot(driver);


        //关闭当前页面窗口(只有一个窗口标签时，相当于river.quit();）注意：窗⼝关闭后driver要重新定义

        /*driver.findElement(By.xpath("//*[@id=\"s-top-left\"]/a[6]")).click();
        driver.close();//关闭的是“百度一下”页面而非"百度图片"  注意：窗⼝关闭后driver要重新定义*/

        //隐式等待(作用全局，但是无法等待非页面元素，如前进后退、弹窗。。。。）

        //driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));

        //显式等待（可以等待非页面元素，但是只作用于当前操作）

        //WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(3));
        //wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"2\"]/div/div/div/div[1]/h3/a/span")));

        //显示等待和隐式等待一起用（可以配合使用，不可混合使用）

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        System.out.println(sdf.format(System.currentTimeMillis()));
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));

        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(6));

        try {
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"2\"]/div/div_/div/div[1]/h3/a/span")));
        }catch (Exception e){

        }
        System.out.println(sdf.format(System.currentTimeMillis()));

        //关闭浏览器
        driver.quit();
    }

    public void getScreenShot(WebDriver driver) throws IOException  {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("HH-mm-ss-SS");

        String dirtime = sdf1.format(System.currentTimeMillis());
        String filetime = sdf2.format(System.currentTimeMillis());

        String filename = "./src/test/images/" + dirtime + "/" + filetime + ".png";
        File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file,new File(filename));


    }
}
